package com.crm.business.crm.api;

import com.alibaba.fastjson.JSONObject;
import com.crm.business.crm.service.ICrmAgreementService;
import com.crm.common.ka.core.common.R;
import com.crm.common.ka.core.common.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.websocket.server.PathParam;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

//协议签署
@Slf4j
@RestController
@RequestMapping("/api/agreement")
public class BillAgreementApiController {


    @Autowired
    private ICrmAgreementService crmAgreementService;

    /**
     * 合同签署回调
     * @return
     */
    @RequestMapping(value = "/signAgreementCallBack/{contractNo}", method = {RequestMethod.GET, RequestMethod.POST})
    public String signContractCallBack(@PathVariable("contractNo") String contractNo, HttpServletRequest request) throws Exception {

        Map<String, String> params = new HashMap<>();
        Map requestParams = request.getParameterMap();
        for (Iterator iter = requestParams.keySet().iterator(); iter.hasNext();) {
            String name = (String) iter.next();
            String[] values = (String[]) requestParams.get(name);
            String valueStr = "";
            for (int i = 0; i < values.length; i++) {
                valueStr = (i == values.length - 1) ? valueStr + values[i]
                        : valueStr + values[i] + ",";
            }
            //乱码解决，这段代码在出现乱码时使用。如果mysign和sign不相等也可以使用这段代码转化
            //valueStr = new String(valueStr.getBytes("ISO-8859-1"), "UTF-8");
            params.put(name, valueStr);
        }
        log.info("爱签合同签署回调参数："+ JSONObject.toJSONString(params));
        String signTime = params.get("signTime");
        return crmAgreementService.signAgreementCallBack(contractNo,signTime);
    }

    @RequestMapping(value = "/upAgreementNumCache", method = {RequestMethod.GET, RequestMethod.POST})
    public Result upAgreementNumCache( @RequestParam("upNum") Integer upNum, @RequestParam("type") Integer type){
        crmAgreementService.upAgreementNumCache(upNum,type);
        return R.ok();
    }
}
